package com.zcxy360.sandtable.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zcxy360.sandtable.domain.CourseStudent;
import com.zcxy360.sandtable.service.CourseStudentService;

/**
 * 学生接口
 * @author 盛亚辉
 *
 */
@RequestMapping("course/student")
@RestController
public class CourseStudentController {

	@Autowired
	private CourseStudentService courseStudentService;
	
	/**
	 * 获取学生
	 * @param courseId 课程id
	 * @return 学生对象
	 */
	@RequiresAuthentication 
	@GetMapping("{courseId}")
	public Object getByCourseId(@PathVariable("courseId")  Integer courseId) {
		return courseStudentService.getByCourseId(courseId);
	}
	
	@GetMapping("getForCompletion")
	public Object getForCompletion(@RequestParam("courseId") Integer courseId,@RequestParam("userId") Integer userId) {
		return courseStudentService.getForCompletion(courseId,userId);
	}
	
	/**
	 * 获取学生集合
	 * @param courseStudent 学生字段
	 * @param key 关键字
	 * @param orderByClause 排序规则
	 * @param pageNum 页码
	 * @param pageSize 页数
	 * @param navigatePages 导航数
	 * @return 学生集合
	 */
	@RequiresAuthentication 
	@GetMapping
	public Object list(CourseStudent courseStudent,String key,String orderByClause,
			@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,
			@RequestParam(defaultValue = "5") Integer navigatePages) {
		return courseStudentService.list(courseStudent,key,orderByClause,pageNum,pageSize,navigatePages);
	}
	
	/**
	 * 新增学生
	 * @param courseStudent 学生字段
	 * @return 学生对象
	 */
	@RequiresRoles(value={"teacher","student"},logical=Logical.OR)
	@PostMapping
	public Object post(CourseStudent courseStudent) {
		return courseStudentService.post(courseStudent);
	}
	
	/**
	 * 更新学生
	 * @param courseStudent 学生字段
	 * @return 布尔值
	 */
	@RequiresRoles(value={"teacher","student"},logical=Logical.OR)
	@PutMapping
	public Object update(CourseStudent courseStudent) {
		return courseStudentService.update(courseStudent);
	}
	
	/**
	 * 删除学生
	 * @param keys 课程id_用户id的数组
	 * @return 布尔值
	 */
	@RequiresRoles("teacher")
	@DeleteMapping("{keys}")
	public Object delete(@PathVariable("keys") String[] keys) {
		return courseStudentService.delete(keys);
	}
	
	/**
	 * 加入课程
	 * @param courseId 课程id
	 * @return 布尔值
	 */
	@RequiresRoles("student")
	@PostMapping("join")
	public Object join(@RequestParam("courseId") Integer courseId) {
		return courseStudentService.join(courseId);
	}
	
	/**
	 * 发布分数
	 * @param courseStudents 学生json字符串
	 * @return 布尔值
	 */
	@RequiresRoles("teacher")
	@PutMapping("mark")
	public Object mark(@RequestParam("courseStudentJson") String courseStudentJson) {
		return courseStudentService.mark(courseStudentJson);
	}
}
